/**
 * Created by Administrator on 2016/1/7.
 */
$(function(){
    var pos;
    var $audio;
    var plist;
    var endHandler = function(){
        if( pos+2 >plist.length){
            pos=-1;
        }
        pos++;
        $audio.attr("src",plist[pos].url);
        $audio[0].play();
    };
    $("#shoot").on("click",function(){

    });
    $(document).on("pagehide",'#signin_html',function(data){
        removeSnow();
        $audio = $("#audioPlayer");
        $audio[0].pause();
        $audio[0].removeEventListener("ended",endHandler);
    });
    $(document).on("pageshow",'#signin_html',function(data){
        var $p = $("#signin_html");
        $audio = $("#audioPlayer");
        var wrapper =  $(".swiper-wrapper");
        $.each(command.underMsg,function(i,d){
            var html = [
                '<div xmlns="http://www.w3.org/1999/xhtml" class="swiper-slide"',
                'style="width: 1024px; height: 164px;">',
                '    <div class="slogen-row clearfix">',
                '        <p>'+d+'</p>',
                '    </div>',
                '</div>'
            ].join("");
            wrapper.append(html);
        });
        //读取localstorage里面的用户
        var localStr = localStorage.getItem("signs");
        if(localStr){
            var usersArr = localStr.split(",");
            //并且上墙
            var wall = $("#signin-content","#signin_html");
            $.each(usersArr,function(i,d){
                var html = [
                        '<div class="signin-item signin-item-show" nm="'+d+'">',
                    '    <div class="signin-item-img">',
                    '        <img ',
                        '        src="images/photo/'+ $.trim(d)+'.jpg">',
                    '    </div>',
                        '    <span class="signin-item-text">'+d+'</span>',
                    '</div>  '
                ].join("");
                //展示上墙动画
                if($("#signin_html").find(".signin-item[nm='"+d+"']").length==0){
                    wall.append(html);
                }
            });
            $(".signin-header-num").html(usersArr.length);
        }

        //签到轮询
        $.signin();
       $.pollMsg();
       setTimeout(function(){
           new Swiper('.swiper-container',{autoplay:3000});
       },1000);
        damoo.show();
        var damooPosition=0;
        setInterval(function(){
            if( damooPosition + 2 > command.autoDamoo.length){
                damooPosition = -1;
            }
            damooPosition++;
            damoo.emit({
                text:command.autoDamoo[damooPosition]
            });
        },10000);

        plist = [
            command.audios[0],
            command.audios[1],
            command.audios[2],
            command.audios[3],
            command.audios[4]
        ];
        pos =  Math.floor(Math.random()*plist.length);
        $audio.removeAttr("loop");
        $audio.attr("src",plist[pos].url);
        ///createSnow('', 200);

        $audio[0].addEventListener("ended",endHandler);
        $audio[0].play();
    })
});
